﻿@{
    ViewBag.Title = "推送组列表";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <script src="/Scripts/date.format.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            InitList();
        });
        function btnSearch_Click() {
            var g = $("#txt_GroupName").val();
            var postUrl = "/ViewGroup/List/?groupName=" + encodeURI(g) + "&dbName=" + encodeURI($.getUrlParam("dbName"));
            InitList(postUrl);
        }

        function btnAdd_Click() {
            AddGroupItem();
        }
        function btnBack_Click() {
            var url = "/EtlTarget/EtlTargetList?dbName=" + encodeURI($.getUrlParam("dbName"));
            if ($.getUrlParam("DBType") == "2")
                url += "&DBType=" + $.getUrlParam("DBType");
            window.location.href = url;
        }
        function InitList(postUrl) {
            if (typeof (postUrl) == "undefined" || postUrl.length == 0) {
                postUrl = "/ViewGroup/List?dbName=" + encodeURI($.getUrlParam("dbName"));
            }
            $("#DataGrid").dg({
                rows: 15,
                height: 400,
                url: postUrl,
                para: { systemID: $.getUrlParam("systemID") },
                idField: "GroupID",
                checkbox: false,
                rownumbers: true,
                columns: [
                    { field: 'GroupName', title: '推送组名称', width: 25, sortable: true,
                        formatter: function (value, rowData, rowIndex) {
                            var format = "";
                            if (rowData.GroupType == 1) {
                                //images/tree_folder_open.gif
                                format = "<img src='/images/tree_folder.gif'> <a href='#' onclick='openViewModelInGroup(\"" + rowData.GroupID + "\");'>" + value + "</a>";
                            }
                            else {
                                format = "<nobr><img src='/images/tree_file.gif' height='16px'> " + value + "</nobr>";
                            }
                            return format;
                        }
                    },
                    { field: 'Description', title: '描述', width: 30, sortable: true },
                    { field: 'CreateTime', title: '创建时间', width: 10, align: 'center', sortable: true,
                        formatter: function (value, rowData, rowIndex) {
                            var format = eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)")).format('yyyy-mm-dd');
                            return format;
                        }
                    },
                    { field: 'list_action', title: '操作', width: 10, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var format = "<select style='width: 100px; '";
                            if (rowData.GroupType == 1) {
                                format += "onchange='onGroupDoAction(this.options.selectedIndex,\"" + rowData.GroupID + "\")'><option>-请选择-</option> <option>导出建表语句</option> <option>添加模型</option> <option>推送配置</option> <option>修改</option> <option>删除</option>";
                            }
                            else {
                                format += "onchange='onModelDoAction(this.options.selectedIndex,\"" + rowData.GroupID + "\")'><option>-请选择-</option> <option>导出建表语句</option> <option>预览表信息</option>"
                            }
                            format += "</select>";
                            return format;
                        }
                    }
                ]
            });
        }
        // 推送组事件
        function onGroupDoAction(i, value) {

        }
        // 模型事件
        function onModelDoAction(i, value) {
            if (i == 1) {
                //  导出建表语句
                exportViewCreateTableScript(value);
            }
            else if (i == 2) {
                // 预览表信息
                openViewModelData(value);
            }
        }
        function exportGroupCreateTableScript(groupID) {
            var url = "/ViewGroup/ExportCreateTableScriptByGroupID?groupID=" + groupID + "&systemID=" + $.getUrlParam("systemID") + "&dbName=" + encodeURI($.getUrlParam("dbName"));
            downloadfile(url, null);
        }

        function exportViewCreateTableScript(viewID) {
            var url = "/ViewGroup/ExportCreateTableScriptByViewID?viewID=" + viewID + "&systemID=" + $.getUrlParam("systemID") + "&dbName=" + encodeURI($.getUrlParam("dbName"));
            downloadfile(url, null);
        }

        function AddGroupItem() {
            var url = "/ViewGroup/Add?systemID=" + $.getUrlParam("systemID") + "&dbName=" + encodeURI($.getUrlParam("dbName"));
            if ($.getUrlParam("DBType") == "2")
                url += "&DBType=" + $.getUrlParam("DBType");
            window.location.href = url;
        }
        function EditGroupItem(groupID) {
            var url = "/ViewGroup/Add?groupID=" + groupID + "&systemID=" + $.getUrlParam("systemID") + "&dbName=" + encodeURI($.getUrlParam("dbName"));
            if ($.getUrlParam("DBType") == "2")
                url += "&DBType=" + $.getUrlParam("DBType");
            window.location.href = url;
        }
        function DeleteGroupItem(groupID) {
            if (confirm("确定要删除吗?")) {
                $.post("/ViewGroup/Delete?groupID=" + groupID + "&systemID=" + $.getUrlParam("systemID") + "&dbName=" + encodeURI($.getUrlParam("dbName")), function (result) {
                    if (result.Success == true) {
                        alert(result.Message);
                    }
                    else {
                        alert(result.Message);
                    }
                    InitList();
                });
            }
        }
        function selectModelToGroup(groupID) {
            var systemID = $.getUrlParam("systemID");
            var url = '/ViewGroup/ModelList?systemID=' + systemID + '&dbName=' + encodeURI($.getUrlParam("dbName"));
            showModalMany('SelectTargetList', url, '810', '460',
                function (result) {
                    var viewIDs = new Array();
                    if (result.length > 0) {
                        for (var i = 0; i < result.length; i++) {
                            var value = result[i].Guid;
                            viewIDs.push(value);
                        }
                        addModelsToGroup(groupID, viewIDs);
                    }
                });
            InitList();
        }

        function addModelsToGroup(groupID, viewIDArray) {
            var systemID = $.getUrlParam("systemID");
            var dbName = encodeURI($.getUrlParam("dbName"));
            var viewIDs = viewIDArray.toString();

            var postUrl = "/ViewGroup/AddModelsToGroup?dbName=" + dbName;
            var postData = { systemID: systemID, groupID: groupID, viewIDs: viewIDs };
            $.ajax({
                type: "POST",
                async: true,
                url: postUrl,
                data: postData,
                success: function (result) {
                    if (result.Success == true) {
                        alert(result.Message);
                    }
                    else {
                        alert(result.Message);
                    }
                    InitList();
                }
            });
        }
        function openViewModelInGroup(groupID) {
            var systemID = $.getUrlParam("systemID");
            var url = '/ViewGroup/ViewModelInGroup?systemID=' + systemID + "&groupID=" + groupID + '&dbName=' + encodeURI($.getUrlParam("dbName"));
            if ($.getUrlParam("DBType") == "2")
                url += "&DBType=" + $.getUrlParam("DBType");
            window.location = url;
        }
        function openViewModelPushConfig(groupID) {
            var systemID = $.getUrlParam("systemID");
            var url = '/ViewGroup/ViewModelPushConfig?systemID=' + systemID + "&groupID=" + groupID + '&dbName=' + encodeURI($.getUrlParam("dbName"));
            if ($.getUrlParam("DBType") == "2")
                url += "&DBType=" + $.getUrlParam("DBType");
            showModalMany('ViewModelPushConfig', url, '810', '460');
            InitList();
        }
        // 预览模型数据
        function openViewModelData(vcid) {
            var id = $.getUrlParam("systemID");
            if (typeof (id) == "undefined" || id == "") {
                id = $("#hddEtlTargetId").val();
            }
            showModalMany('Contact', '/EtlTarget/ViewModelData?systemID=' + id + "&viewID=" + vcid + "&dbName=" + encodeURI($.getUrlParam("dbName")), '800', '450');
            InitList();
        }

        // Ajax 文件下载 
        function downloadfile(url, data) {
            if (url || data) {
                var inputs = '';
                if (typeof data !== "undefined" && data != null) {
                    $.each(data, function () {
                        var pair = this.split('=');
                        inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
                    });
                }
                $('<form action="' + url + '" method="post">' + inputs + '</form>').appendTo('body').submit().remove();
            };
        }
    </script>
}
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/search.png" />
            </td>
            <td width="300">
                推送组搜索
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%">
        <tr>
            <td>
                推送组名称：
                <input id="txt_GroupName" type="text" class="inputSearch" style="width: 120px;" />
                <input id="btnSearch" type="button" value=" 搜索 " onclick="btnSearch_Click()" />
            </td>
        </tr>
    </table>
</div>
<div style="margin: 5px; min-height: 300px;">
    @{
        // 渲染输出列表
        var dataGrid = new ViewDataDictionary();
        dataGrid.Add("PanelTitle", "推送组列表");
        dataGrid.Add("GridID", "DataGrid");
        dataGrid.Add("ShowAddButton", true);
        dataGrid.Add("ShowBackButton", true);
        dataGrid.Add("ShowPager", true);
        dataGrid.Add("ShowLoading", true);
        @Html.Partial("_GridPanel", dataGrid);
    }
    @*<table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="300">
                推送组列表
            </td>
            <td align="right">
                <input id="btnAdd" type="button" value=" 新增推送组 " class="button" onclick="btnAdd_Click()" />
                <input id="btnBack" type="button" value=" 返回 " class="button" onclick="btnBack_Click()" />
            </td>
        </tr>
    </table>
    <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
    </table>
    <div>
        <div id="Pagination" class="pagination">
        </div>
        <div id="loading" class="loading">
            <img src="/Images/tree_loading.gif" alt="正在加载中..." />
        </div>
    </div>
    <div id="msg">
    </div>*@
</div>
